﻿Public Class DirectConnectForm

    Private _Canceled As Boolean = False

    Private _load As Boolean = False

    Private Const COMPORT_KEYNAME As String = "TBCC_COMNumber"

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Cursor = Cursors.Default

        If Me.btnDirectConnect.Enabled = False Then

            Me.btnDirectConnect.Enabled = True
        Else
            Me.DialogResult = Windows.Forms.DialogResult.Cancel
        End If

        _Canceled = True
        'Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub DirectConnectForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        init()
        'Timer1.Interval = 1000
        'Timer1.Start()
        _load = True
    End Sub

    Private Sub Init()
        COMHelper.FillComName(Me.cboCOM)

        Dim port As Integer = 0
        Try
            port = My.Computer.Registry.CurrentUser.GetValue(COMPORT_KEYNAME)
        Catch ex As Exception
            Debug.WriteLine(ex.Message)
        End Try

        Me.cboCOM.SelectedIndex = Me.cboCOM.FindString("COM" & port.ToString())
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Timer1.Stop()
        'Connect()
    End Sub

    Private Sub Connect()
        If 1 = 1 Then
            Me.DialogResult = Windows.Forms.DialogResult.OK
            Exit Sub
        End If
        Dim result As Boolean = False

        While result = False AndAlso _Canceled = False
            result = COMHelper.Instance.ReplyCommand("AT", "OK", 1000 * 1)
            Application.DoEvents()
            Application.DoEvents()
        End While

        If result = False Then
            ' Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Else
            Me.DialogResult = Windows.Forms.DialogResult.OK
        End If
    End Sub

    Private Sub btnDirectConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDirectConnect.Click
        Me.btnDirectConnect.Enabled = False

        Dim port As Integer
        port = cboCOM.Text.Replace("COM", "")

        Dim result As Boolean
        'result = COMHelper.Instance.InitSerialPort(port, IO.Ports.Parity.Even)
        result = COMHelper.Instance.InitSerialPort(port, IO.Ports.Parity.None)

        If result = True Then
            Me.Cursor = Cursors.WaitCursor
            Me.Connect()
            'Me.DialogResult = Windows.Forms.DialogResult.OK
            'Me.Close()
        Else
            msg.ShowError("连接串口失败，请选择正确的串口并检查连接。")
        End If

    End Sub

    Private Sub cboCOM_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCOM.SelectedIndexChanged
        If _load = False Then
            Exit Sub
        End If

        Dim port As Integer
        port = Integer.Parse(Me.cboCOM.Text.Replace("COM", ""))
        My.Computer.Registry.CurrentUser.SetValue(COMPORT_KEYNAME, port)
    End Sub
End Class